//
//  PurchaseCarView.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/6/18.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import UIKit
class PurchaseCarView: UIView {
    let labelW:CGFloat = 60
    var carType,price,buyerName,buyerPhone:UITextField!
    var submitPurchase:UIButton!
    var remark:UITextView!
    var carTypeText = "",priceText = "",nameText = "",phoneText = "",remarkText = "",ostatus = ""
    var purchType = "add"//当前页面类型添加，还是修改 默认是添加
    override func layoutSubviews() {
        let carTypeLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: 0, width: labelW, height: cellH))
        carTypeLabel.font = oFontSize
        carTypeLabel.text = "意向车型"
        carTypeLabel.textAlignment = .right
        self.addSubview(carTypeLabel)
        
        carType = UITextField.init(frame: CGRect(x: labelW + 20, y: 0, width: kScreenW - labelW - 20, height: cellH))
        carType.textColor = fontColor
        carType.font = oFontSize
        carType.clearButtonMode = .whileEditing
        carType.becomeFirstResponder()//获取焦点
        carType.placeholder = "可输入多个用\"、\"分割"
        carType.text = carTypeText
        carType.autocapitalizationType = .none //关闭首字母自动大写
        carType.inputAccessoryView = AddToolBar()
        self.addSubview(carType)
        self.addSubview(getSeparator(index: 1))
        
        let mileageLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: 1), width: labelW, height: cellH))
        mileageLabel.font = oFontSize
        mileageLabel.text = "价格范围"
        mileageLabel.textAlignment = .right
        self.addSubview(mileageLabel)
        
        price = UITextField.init(frame: CGRect(x: labelW + 20, y: getCellY(index: 1), width: kScreenW - labelW - 20, height: cellH))
        price.textColor = fontColor
        price.font = oFontSize
        price.clearButtonMode = .whileEditing
        price.placeholder = "请输入价格范围"
        price.text = priceText
        price.autocapitalizationType = .none //关闭首字母自动大写
        price.inputAccessoryView = AddToolBar()
        self.addSubview(price)
        self.addSubview(getSeparator(index: 2))
        
        let buyerLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: 2), width: labelW, height: cellH))
        buyerLabel.font = oFontSize
        buyerLabel.text = "买主姓名"
        buyerLabel.textAlignment = .right
        self.addSubview(buyerLabel)
        
        buyerName = UITextField.init(frame: CGRect(x: labelW + 20, y: getCellY(index: 2), width: kScreenW - labelW - 20, height: cellH))
        buyerName.textColor = fontColor
        buyerName.font = oFontSize
        buyerName.clearButtonMode = .whileEditing
        buyerName.placeholder = "请输入买主姓名"
        buyerName.text = nameText
        buyerName.autocapitalizationType = .none //关闭首字母自动大写
        buyerName.inputAccessoryView = AddToolBar()
        self.addSubview(buyerName)
        self.addSubview(getSeparator(index: 3))
        
        let buyerPhoneLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: 3), width: labelW, height: cellH))
        buyerPhoneLabel.font = oFontSize
        buyerPhoneLabel.text = "联系电话"
        buyerPhoneLabel.textAlignment = .right
        self.addSubview(buyerPhoneLabel)
        
        buyerPhone = UITextField.init(frame: CGRect(x: labelW + 20, y: getCellY(index: 3), width: kScreenW - labelW - 20, height: cellH))
        buyerPhone.textColor = fontColor
        buyerPhone.font = oFontSize
        buyerPhone.clearButtonMode = .whileEditing
        buyerPhone.placeholder = "请输入买主联系电话"
        buyerPhone.text = phoneText
        buyerPhone.autocapitalizationType = .none //关闭首字母自动大写
        buyerPhone.inputAccessoryView = AddToolBar()
        self.addSubview(buyerPhone)
        self.addSubview(getSeparator(index: 4))
        
        let remarkLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: 4), width: oLabelW, height: 2*cellH))
        remarkLabel.font = oFontSize
        remarkLabel.text = "备注信息"
        self.addSubview(remarkLabel)
        
        remark = UITextView(frame:CGRect(x:oLabelW, y:getCellY(index: 4) + 10, width:oImgW, height:remarkH))
        remark.layer.cornerRadius = 5
        remark.backgroundColor = tableBgColor
        remark.textColor = fontColor
        remark.font = oFontSize
        remark.text = remarkText
        remark.inputAccessoryView = AddToolBar()
        self.addSubview(remark)
        self.addSubview(getSeparator(index: 6))
        submitPurchase = UIButton(frame:CGRect(x: oLabelLeft, y: getCellY(index: 7), width: kScreenW - 2*oLabelLeft, height: cellH))
        submitPurchase.backgroundColor = appMainColor
        submitPurchase.showsTouchWhenHighlighted = true
        submitPurchase.layer.cornerRadius = 10;
        if "add" == purchType{//添加的时候
            let pcc = PurchaseCarController()
            submitPurchase.setTitle("提交", for: .normal)
            submitPurchase.addTarget(pcc, action: #selector(pcc.submitPurchClick), for: .touchUpInside)
        }else{//编辑
            carType.isEnabled = false
            price.isEnabled = false
            buyerName.isEnabled = false
            buyerPhone.isEnabled = false
            remark.isEditable = false
            if "1" == isEmployee {//普通用户登录 新车代购不需要服务费 故 ostatus 不会为0
                if("1" == ostatus){
                    submitPurchase.setTitle("等待处理", for: .normal)
                    submitPurchase.backgroundColor = UIColor.lightGray
                }else if("2" == ostatus){
                    submitPurchase.setTitle("处理中", for: .normal)
                    submitPurchase.backgroundColor = UIColor.lightGray
                }else if("3" == ostatus){
                    submitPurchase.setTitle("已处理", for: .normal)
                    submitPurchase.backgroundColor = UIColor.lightGray
                }
            }
            if("2" == isEmployee){
                let pic = PurchaseInfoController()
                if "1" == ostatus {
                    submitPurchase.setTitle("抢单", for: .normal)
                    submitPurchase.addTarget(pic, action: #selector(pic.acceptFun), for: .touchUpInside)
                }
                if "2" == ostatus {
                    submitPurchase.setTitle("完成", for: .normal)
                    submitPurchase.addTarget(pic, action: #selector(pic.finishFun), for: .touchUpInside)
                }
                if "3" == ostatus {
                    submitPurchase.setTitle("已完成", for: .normal)
                    submitPurchase.backgroundColor = UIColor.lightGray
                    submitPurchase.isEnabled = false
                }
            }
        }
        self.addSubview(submitPurchase)
    }
}
extension PurchaseCarView{
    func getCellY(index : Int) -> CGFloat {
        return cellH * CGFloat(index)
    }
    func getSeparator(index: Int) -> UILabel {
        let lab = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: index), width: kScreenW - 2*oLabelLeft, height: 1))
        lab.backgroundColor = separatorColor
        return lab
    }
    //点击空白回收键盘
    func hideKeyBoard() {
        self.endEditing(true)
    }
    func AddToolBar() -> UIToolbar {
        let toolBar:UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: kScreenW, height: 35))
        toolBar.backgroundColor = UIColor.gray
        let spaceBtn = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let barBtn = UIBarButtonItem(title: "完成", style: .plain, target: self, action: #selector(hideKeyBoard))
        toolBar.items = [spaceBtn, barBtn]
        return toolBar
    }
}
